import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class Main {

    // 增加项目
    public static void main(String[] args) throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/subjectlyw?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("1234");

        Connection connection = dataSource.getConnection();

        String sql = "insert into subject value(?,?)";
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入题目");
        String name = scanner.next();
        System.out.println("请输入题目描述");
        String desc = scanner.next();
        PreparedStatement statement = connection.prepareStatement(sql);//预编译
        statement.setString(1,name);
        statement.setString(2,desc);
        int ret = statement.executeUpdate();// update insert delete
        System.out.println(statement);
        //5.释放必要的资源
        statement.close();
        connection.close();
    }
}
